﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.SystemManager;
using CubicFWNet.Data.ViewModal;

namespace CubicFWNet.Application.SystemManager
{
    public class ProductApp
    {
        private ProductRepository _productRepository;
        private TechnologyManageRepository _technologyManageRepository;
        private CategoryRepository _categoryRepository;


        public ProductApp()
        {
            _productRepository = new ProductRepository();
            _technologyManageRepository = new TechnologyManageRepository();
            _categoryRepository = new CategoryRepository();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="inu"></param>
        /// <returns></returns>
        public Product GetByNumber(string number)
        {
            return _productRepository.IQueryable(a => a.Number == number).FirstOrDefault();
        }



        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="inu"></param>
        /// <returns></returns>
        public int Add(Product pro)
        {
            return _productRepository.Insert(pro);
        }

        /// <summary>
        /// 获取产品或原料列表(未维护的)
        /// </summary>
        /// <param name="type">1成品   2主料   3辅料</param>
        /// <param name="status"></param>
        /// <param name="name"></param>
        /// <param name="num"></param>
        /// <returns></returns>
        public IEnumerable<Product> GetFuckList(int type, int status, string name, string num)
        {
            List<Guid> guidlist = _technologyManageRepository.IQueryable().Select(b => b.ProductId).ToList();
            return _productRepository.IQueryable(a => !guidlist.Contains(a.Id) && (status == 0 || a.Status == status) && a.Name.Contains(name) && a.Number.Contains(num)).AsQueryable().OrderByDescending(a => a.CreateTime);
        }
        //已维护的
        public IEnumerable<Product> GetListBack(int type, int status, string name, string num)
        {
            List<Guid> guidlist = _technologyManageRepository.IQueryable().Select(b => b.ProductId).ToList();
            return _productRepository.IQueryable(a => guidlist.Contains(a.Id) && (status == 0 || a.Status == status) && a.Name.Contains(name) && a.Number.Contains(num)).AsQueryable().OrderByDescending(a => a.CreateTime);
        }
        /// <summary>
        /// 获取产品列表[销售员查看，正常状态+审核中]
        /// </summary>
        /// <returns></returns>
        public IEnumerable<ProductView> GetList1(int categoryid, string name, string width, string length)
        {
            var query = from a in _productRepository.IQueryable()
                        join nt in _categoryRepository.IQueryable()
                        on a.CategoryId equals nt.Id
                        into t
                        from x in t.DefaultIfEmpty()
                        where (categoryid == 0 || a.CategoryId == categoryid) && (a.Name.Contains(name) || a.Number.Contains(name)) && (width == "" || a.SizeWidth.Contains(width)) && (length == "" || a.SizeLenth.Contains(length))
                        orderby a.CreateTime descending
                        select new ProductView
                        {
                            CategoryName = x != null ? x.Name : "",
                            Id = a.Id,
                            Name = a.Name,
                            Number = a.Number,
                            //Type = a.Type,
                            Price = a.Price,
                            CustomerName = a.CreatorName,
                            CreateTime = a.CreateTime,
                            Memo = a.Memo,
                            SizeWidth = a.SizeWidth,
                            SizeLenth = a.SizeLenth,
                            Unit = a.Unit,
                            Format = a.Format,
                            ExecuteDepartment = a.ExecuteDepartment,
                            Status = a.Status
                        };
            return query;
        }
        /// <summary>
        /// 获取产品列表[销售员查看，正常状态+审核中]
        /// </summary>
        /// <returns></returns>
        public IEnumerable<ProductView> GetList(int categoryid, string name)
        {
            var query = from a in _productRepository.IQueryable()
                        join nt in _categoryRepository.IQueryable()
                        on a.CategoryId equals nt.Id
                        into t
                        from x in t.DefaultIfEmpty()
                        where (categoryid == 0 || a.CategoryId == categoryid) && (a.Name.Contains(name) || a.Number.Contains(name))
                        orderby a.CreateTime descending
                        select new ProductView
                        {
                            CategoryName = x != null ? x.Name : "",
                            Id = a.Id,
                            Name = a.Name,
                            Number = a.Number,
                            //Type = a.Type,
                            Price = a.Price,
                            CustomerName = a.CreatorName,
                            CreateTime = a.CreateTime,
                            Memo = a.Memo,
                            Unit = a.Unit,
                            Format = a.Format,
                            ExecuteDepartment = a.ExecuteDepartment,
                            Status = a.Status
                        };
            return query;
        }

        /// <summary>
        /// 获取原料列表
        /// </summary>
        /// <param name="type">0全部 1主料   2原料</param>
        /// <param name="status"></param>
        /// <param name="name"></param>
        /// <param name="num"></param>
        /// <returns></returns>
        public IEnumerable<ProductView> GetMaterialList(int type, int status, string name, string num)
        {
            var query = from a in _productRepository.IQueryable()
                        join nt in _categoryRepository.IQueryable()
                        on a.CategoryId equals nt.Id
                        where (status == 0 || a.Status == status) && a.Name.Contains(name) && a.Number.Contains(num)
                        orderby a.CreateTime descending
                        select new ProductView
                        {
                            CategoryName = nt.Name,
                            Id = a.Id,
                            Name = a.Name,
                            Number = a.Number,
                            //Type = a.Type,
                            Price = a.Price,
                            CustomerName = a.CreatorName,
                            CreateTime = a.CreateTime,
                            Status = a.Status
                        };
            return query;
        }

        /// <summary>
        /// 获取产品信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Product GetInfo(Guid id)
        {
            return _productRepository.FindEntity(a => a.Id == id);
        }

        /// <summary>
        /// 添加产品
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        public int Insert(Product product)
        {
            return _productRepository.Insert(product);
        }

        /// <summary>
        /// 更新产品
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        public int Update(Product product)
        {
            return _productRepository.Update(product);
        }

        /// <summary>
        /// 根据订单号获得包涵的所有产品信息
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public IQueryable<ProductTechnologyView> GetProductListByOrderId(Guid orderId)
        {
            return _productRepository.GetProductListByOrderId(orderId);
        }

        /// <summary>
        /// 获取销售员名下的产品数量
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public int GetProductCount(Guid uid)
        {
            return _productRepository.IQueryable(a => a.CreatorId == uid).Count();
        }

    }
}
